//
//  detailViewController.swift
//  Test
//
//  Created by CAO-YASUO on 2021/3/27.
//

import UIKit

class DetailViewController: UIViewController {
    
    var imageView : UIImageView!
    var scrollView : UIScrollView!
    var didScale = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupScrollView()
        // Do any additional setup after loading the view.
    }
    
    func setupImageView()->UIImageView{
        let detailView = UIImageView(image: characterArray[cct].insectImage)
        return detailView
    }
    
    func setupScrollView(){
        scrollView = UIScrollView(frame: UIScreen.main.bounds)
        //背景色
        scrollView.layer.backgroundColor = CGColor(red: 0, green: 0, blue: 0, alpha: 1)
        //添加单击消失手势
        let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTap(tap:)))
        singleTap.numberOfTapsRequired = 1
        scrollView.addGestureRecognizer(singleTap)
        //添加双击缩放手势
        let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTap(tap:)))
        doubleTap.numberOfTapsRequired = 2
        scrollView.addGestureRecognizer(doubleTap)
        singleTap.require(toFail: doubleTap)
        self.view.addSubview(scrollView)
        imageView = setupImageView()
        //1、允许拖拽
        scrollView.contentSize = imageView.bounds.size
        //2、允许缩放
        scrollView.delegate = self
        scrollView.addSubview(imageView)
        //设置图片最小因子，缩小图片
        let scale = UIScreen.main.bounds.width/imageView.bounds.width
        scrollView.maximumZoomScale = 5
        scrollView.minimumZoomScale = scale
        scrollView.zoomScale = scale
    }
    
    @objc func singleTap(tap:UIGestureRecognizer){
        if tap.state == .ended{
            dismiss(animated: true, completion: nil)
        }
    }
    @objc func doubleTap(tap:UIGestureRecognizer){
        if tap.state == .ended{
            if didScale == false{
                scrollView.setZoomScale(2, animated: true)
                didScale = true
            }else{
                scrollView.setZoomScale(1, animated: true)
                didScale = false
            }
            
        }
    }
    
    func calcHeight()->CGFloat{
        if UIScreen.main.bounds.height >= imageView.frame.height{
            let result = (UIScreen.main.bounds.height - imageView.frame.height)/2
            return result
        }else{
            return 0
        }
    }
    func calcWidth()->CGFloat{
        if UIScreen.main.bounds.width >= imageView.frame.width{
            let result = (UIScreen.main.bounds.width - imageView.frame.width)/2
            return result
        }else{
            return 0
        }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension DetailViewController : UIScrollViewDelegate{
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        let upDown = calcHeight()
        let LR = calcWidth()
//        let upDown = (UIScreen.main.bounds.height - imageView.frame.height)/2
        scrollView.contentInset = UIEdgeInsets(top: upDown, left: LR, bottom: upDown, right: LR)
    }
}

